﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using BookShelf.Model.Domain;

namespace BookShelf.Model
{
    public class Book : Entity<Book, int>
	{

		#region Private Members

		private List<Author> authors = new List<Author>();

		#endregion

		#region Public and Protected Members

        public override int ID { get; set; }
        public string Isbn { get; protected set; }
        public string Title { get; protected set; }
        public DateTime PublicationDate { get; protected set;}
        public int Pages { get; protected set; }
		public string Catagory { get; protected set; }

		#endregion

		public ReadOnlyCollection<Author> Authors
        {
            get { return authors.AsReadOnly(); }
        }
        
        public Book(int id, string isbn, string title, DateTime publicationDate, int pages)
        {

            if (id < 0)
                throw new ArgumentOutOfRangeException("id must be greater then or equal to zero");

            if (string.IsNullOrEmpty(isbn))
                throw new ArgumentNullException("isbn must not be null or empty.");

            if (string.IsNullOrEmpty(title))
                throw new ArgumentNullException("title must not be null or empty.");

            if (publicationDate == DateTime.MinValue)
                throw new ArgumentOutOfRangeException("publicationDate must be greater then " + DateTime.MinValue.ToShortDateString());

            this.ID = id;
            this.Isbn = isbn;
            this.Title = title;
            this.PublicationDate = publicationDate;
            this.Pages = pages;

        }

        public void AddAuthor(Author author)
        {
            authors.Add(author);
        }

        public bool RemoveAuthor(Author author)
        {
            return authors.Remove(author);
        }

		public void SetCategory(string category)
		{
			this.Catagory = category;
		}

        public override bool Equals(Book other)
        {
            return this.Isbn.ToLower() == other.Isbn.ToLower();
        }

    }
}
